#include "otherrowwidget.h"

OtherRowWidget::OtherRowWidget()
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

current_record = QSqlRecord();
current_index = QModelIndex();
is_new = false;

dateEdit->setDate( QDate::currentDate() );

QObject::connect(commitButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(save_clicked()));
QObject::connect(cancelButton,
                 SIGNAL(clicked()),
                 this,
                 SLOT(close()));
}

OtherRowWidget::~OtherRowWidget()
{
}

void OtherRowWidget::set_record( QSqlRecord record,
                                QModelIndex index )
{
    if (!record.isNull("NAME") )
    {
    QString s = record.value("NAME").toString();
    otherNameCheckBox->setChecked(true);
    otherNameLineEdit->setText(s);
    }
    else
    {
    otherNameCheckBox->setChecked(false);
    otherNameLineEdit->clear();
    }

    if (!record.isNull("OTHER_DATE") )
    {
    QDate date = QDate::fromString( QString::number( record.value("AUCT_DATE").toInt() ),
                                    "yyyyMMdd" );
        if ( true == date.isValid() )
        {
        otherDateCheckBox->setChecked(true);
        dateEdit->setDate(date);
        }
    }
    else
    {
    otherDateCheckBox->setChecked(false);
    }

    if (!record.isNull("TEXT_1") )
    {
    QString s = record.value("TEXT_1").toString();
    otherNote1CheckBox->setChecked(true);
    otherNote1LineEdit->setText(s);
    }
    else
    {
    otherNote1CheckBox->setChecked(false);
    otherNote1LineEdit->clear();
    }

    if (!record.isNull("TEXT_2") )
    {
    QString s = record.value("TEXT_2").toString();
    otherNote2CheckBox->setChecked(true);
    otherNote2LineEdit->setText(s);
    }
    else
    {
    otherNote2CheckBox->setChecked(false);
    otherNote2LineEdit->clear();
    }

current_record = QSqlRecord(record);
current_index = QModelIndex(index);
}

QSqlRecord OtherRowWidget::read_record(QSqlRecord record)
{
    if ( otherNameCheckBox->isChecked() )
    {
    record.setValue( "NAME", QVariant( otherNameLineEdit->text() ) );
    }
    else
    {
    record.setNull("NAME");
    }

    if ( otherNote1CheckBox->isChecked() )
    {
    record.setValue("TEXT_1", QVariant( otherNote1LineEdit->text() ) );    }
    else
    {
    record.setNull("TEXT_1");
    }

    if ( otherNote2CheckBox->isChecked() )
    {
    record.setValue("TEXT_2", QVariant( otherNote2LineEdit->text() ) );    }
    else
    {
    record.setNull("TEXT_2");
    }

    if ( otherDateCheckBox->isChecked() )
    {
    QDate d = dateEdit->date();
    QString d_str = d.toString("yyyyMMdd");
    bool ok = false;
    int d_int = d_str.toInt( &ok, 10 );
        if ( true == ok )
        {
        record.setValue("OTHER_DATE", d_int );
        }
        else
        {
        record.setNull("OTHER_DATE");
        }
    }
    else
    {
    record.setNull("OTHER_DATE");
    }

return record;
}


void OtherRowWidget::new_record( qint64 coin_id, QString opened_album )
{

    if ( true == opened_album.isEmpty() )
    {
    return;
    }

QSqlRecord record = QSqlRecord();

qint64 other_id = Q_INT64_C(-1);
    {
    QSqlDatabase db = QSqlDatabase::database("collection");
       if ( !db.isOpen() )
       {
       return;
       }
    QString s = QLatin1String("select gen_id(");
    s.append(opened_album);
    s.append(QLatin1String("_other_gen,1) from rdb$database"));
    QSqlQuery query(s, db);
        while (query.next())
        {
        other_id = query.value(0).toLongLong();
        }
    query.clear();
    }

    if (other_id == -1)
    {
    return;
    }

is_new = true;

record.insert( 0, QSqlField("OTHER_ID", QVariant::LongLong) );
record.setValue("OTHER_ID", other_id);

record.insert( 1, QSqlField("COIN_ID", QVariant::LongLong) );
record.setValue("COIN_ID", coin_id);

record.insert(  2, QSqlField("NAME", QVariant::String) );
record.insert(  3, QSqlField("TEXT_1", QVariant::String) );
record.insert(  4, QSqlField("TEXT_2", QVariant::String) );
record.insert(  5, QSqlField("OTHER_DATE", QVariant::Int) );

current_record = QSqlRecord(record);
}

void OtherRowWidget::save_clicked()
{
QSqlRecord record = read_record(current_record);
emit record_to_commit( record,
                       current_index,
                       QLatin1String("OTHER_ID"),
                       is_new);
close();
}

void OtherRowWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("OtherRowWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void OtherRowWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("OtherRowWidget") );
resize( settings.value( QLatin1String("size"), QSize( 400, 270 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void OtherRowWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}

